<?
/**
*  账号管理
*  2013/7/14 by zh
*/
include("../webkit/include/simple_db.php");

include("session.php");
require("ajax_base.php");

$db = new SimpleDB();
$action = $_GET['action'];

if($action == "del") {
	$db->doDelete('jx_user', 'id='.$_GET['id']); 
	echo "ok";

} else if($action == "list") {
	$condition = "1=1";
	if(isset($_GET['user_status']) && $_GET['user_status']!=0) {
		$condition .= " and user_status=".$_GET['user_status'];
	}
	if(isset($_GET['username'])) {
		$condition .= " and username='".$_GET['username']."'";
	}
	if(isset($_GET['telephone'])) {
		$condition .= " and telephone='".$_GET['telephone']."'";
	}
	$sql = "select id,username,truename,telephone,user_flag, user_status, last_login, login_count from jx_user where $condition order by id desc"; 
	echo to_grid_json($db, $sql, $dic_data, array("username"=>"blue_color_mapper","telephone"=>"red_color_mapper"));

} else if($action == "get") {
	$sql = "select * from jx_user where id=".$_GET['id']; 
	$arr = $db->getOneRecordArray($sql);
	echo array_to_json($arr);

} else if($action == "add") {
	if($db->getOneRecord("select id from jx_user where username='".$_POST['username']."'") != null) {
		echo 'username_exists';
		exit;
	}
	unset($_POST['id']);
	$db->doInsert('jx_user', $_POST);
	echo 'ok';

} else if($action == "edit") {
	$id = $_POST['id'];
	unset($_POST['id']);
	$db->doUpdate('jx_user', $_POST, 'id='.$id);
	echo 'ok';

} else if($action == "get_select_options") {
	$user_flag = 'worker'; // 默认只查询外勤工号
	if(isset($_GET['user_flag'])) {
		$user_flag = $_GET['user_flag']; 
	}
	$arr = array();
	$db->query("select username, truename from jx_user where user_flag='$user_flag' order by id");
	while($db->moveNext()) {
		$arr[] = $db->f("username") .",". $db->f("truename");
	}
	echo join("$", $arr);
}

?>